package com.zzyy.study;


class SingletonV2
{
    private static volatile SingletonV2 instance;

    private SingletonV2()
    {
        System.out.println(Thread.currentThread().getName()+"\t"+"构造方法，只能出现一次");
    }

    public static SingletonV2 getInstance()
    {
        if(instance == null) {
            synchronized (SingletonV2.class) {
                if (instance == null) {
                    instance = new SingletonV2();
                }
            }
        }
        return instance;
    }

    @Override
    public String toString()
    {
        return "SingletonV2 test: "+Thread.currentThread().getName();
    }
}


/**
 * @auther zzyy
 * @create 2024-03-26 11:10
 */
public class T4
{
    public static void main(String[] args)
    {
        /*System.out.println(SingletonV2.getInstance());
        System.out.println(SingletonV2.getInstance());
        System.out.println(SingletonV2.getInstance());*/

        for (int i = 1; i <=3; i++) {
            new Thread(() -> {
                System.out.println(SingletonV2.getInstance());
            },String.valueOf(i)).start();
        }

    }
}
